package com.openxt.nbzj.model;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;

import com.openxt.nbzj.fragment.OpenxtBaseActivity;

import java.lang.ref.WeakReference;

import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.PermissionUtils;

/**
 * @author 作者：apple
 * @date 创建时间 2016/9/14
 * @Description:
 */
public class PermissionsDispatcher {
    private static final int REQUEST_SHOWNESS = 0;
    public static final String[] NESS_PERMISSIONS = new String[]{
            "android.permission.CAMERA",
            "android.permission.READ_PHONE_STATE",
            "android.permission.ACCESS_FINE_LOCATION",
            "android.permission.ACCESS_COARSE_LOCATION",
            "android.permission.WRITE_EXTERNAL_STORAGE",
            "android.permission.RECORD_AUDIO",
            "android.permission.READ_EXTERNAL_STORAGE"};

    public static void initPermissionsDispatcher(OpenxtBaseActivity target) {
        if (PermissionUtils.hasSelfPermissions(target, NESS_PERMISSIONS)) {
            permissionpInited(target);
        } else {
            if (PermissionUtils.shouldShowRequestPermissionRationale(target, NESS_PERMISSIONS)) {
                showRationaleForNess(target, new ShowNessPermissionRequest(target));
            } else {
                ActivityCompat.requestPermissions(target, NESS_PERMISSIONS, REQUEST_SHOWNESS);
            }
        }
    }

    private static final class ShowNessPermissionRequest implements PermissionRequest {
        private final WeakReference<OpenxtBaseActivity> weakTarget;

        private ShowNessPermissionRequest(OpenxtBaseActivity target) {
            this.weakTarget = new WeakReference<>(target);
        }

        @Override
        public void proceed() {
            OpenxtBaseActivity target = weakTarget.get();
            if (target == null) return;
            ActivityCompat.requestPermissions(target, NESS_PERMISSIONS, REQUEST_SHOWNESS);
        }

        @Override
        public void cancel() {
            OpenxtBaseActivity target = weakTarget.get();
            if (target == null)
                return;
            onNessDenied(target);
        }
    }

    public static void onRequestPermissionsResult(OpenxtBaseActivity target, int requestCode, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_SHOWNESS:
                if (!PermissionUtils.hasSelfPermissions(target, NESS_PERMISSIONS)) {
                    onNessDenied(target);
                    return;
                }
                if (PermissionUtils.verifyPermissions(grantResults)) {
                    permissionpInited(target);
                } else if (!PermissionUtils.shouldShowRequestPermissionRationale(target, NESS_PERMISSIONS)) {
                    onNessNeverAskAgain(target);
                } else {
                    onNessDenied(target);
                }
                break;
            default:
                break;
        }
    }

    public static void permissionpInited(Context context) {
    }

    public static void onNessDenied(Context context) {
        Toast.makeText(context, "您已拒绝必要的权限，也许会有未知错误", Toast.LENGTH_SHORT).show();
    }

    public static void onNessNeverAskAgain(Context context) {
        Toast.makeText(context, "请移步到设置-应用-甬城住建-权限中设定相关必要权限", Toast.LENGTH_SHORT).show();
    }

    public static void showRationaleForNess(Context context, PermissionRequest request) {
        showRationaleDialog(context, "1.相机权限\n2.电话权限\n3.位置权限\n4.存储权限\n5.麦克风\n以上是必须权限，请接受授权", request);
    }

    private static void showRationaleDialog(Context context, String messageResId, final PermissionRequest request) {
        new AlertDialog.Builder(context)
                .setTitle("权限赋予")
                .setMessage(messageResId)
                .setPositiveButton("接受授权", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        request.proceed();
                    }
                })
                .setNegativeButton("取消授权", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        request.cancel();
                    }
                })
                .create().show();
    }
}
